/*
 * @Descripttion: antd Table二度封装
 * @version:
 * @Author: spring
 * @Date: 2022-04-18 10:14:43
 * @LastEditors: spring
 * @LastEditTime: 2022-04-18 19:21:01
 */
import { Table } from "antd";
import { memo } from "react";
const { Column } = Table;
interface AntdTableProps {
  tableConfig: Object
  configColumn: Column,
  columnSlot?: Object,
  columnSlotIndex?: number // 插入列的位置，从1开始
  children?: Object // table下的插槽
}
interface Column {
  column: Array<Object>
}
export default memo(function AntdTable(props: AntdTableProps) {
  const { column } = props.configColumn;
  const { tableConfig, columnSlot, columnSlotIndex } = props;
  let columnResult = column
  if (columnSlot && !columnSlotIndex) {
    columnResult = column.concat([''])
  } else if (columnSlot && columnSlotIndex) {
    (columnResult.indexOf('insertedflag') === -1) && columnResult.splice(columnSlotIndex - 1, 0, 'insertedflag')
  }
  return (
    <Table {...tableConfig}>
      {columnResult.map((item: any, index: number) => {
        let slotIndex = !columnSlotIndex ? columnResult.length - 1 : columnSlotIndex - 1
        return index === slotIndex ? columnSlot : <Column {...item} />;
      })}
      {props.children}
    </Table>
  );
});
